/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.ParsedDateTime;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZParameter;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZProperty;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.CalendarDataSource;
/*     */ import com.zimbra.cs.mailbox.calendar.CalendarMailSender;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.RecurId;
/*     */ import com.zimbra.cs.mailbox.calendar.ZOrganizer;
/*     */ import com.zimbra.cs.mime.MimeVisitor;
/*     */ import com.zimbra.cs.mime.MimeVisitor.VisitPhase;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.Enumeration;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.activation.DataHandler;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.Header;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.Part;
/*     */ import javax.mail.internet.ContentType;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ForwardCalendarItem
/*     */   extends CalendarRequest
/*     */ {
/*     */   protected static Account getZDesktopSafeAuthenticatedAccount(ZimbraSoapContext zsc)
/*     */     throws ServiceException
/*     */   {
/*  79 */     Account authAcct = getAuthenticatedAccount(zsc);
/*  80 */     if (AccountUtil.isZDesktopLocalAccount(authAcct.getId())) {
/*  81 */       return getRequestedAccount(zsc);
/*     */     }
/*  83 */     return authAcct;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  88 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  89 */     Account senderAcct = getZDesktopSafeAuthenticatedAccount(zsc);
/*  90 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  91 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*     */ 
/*     */ 
/*  95 */     ItemId iid = new ItemId(request.getAttribute("id"), zsc);
/*  96 */     if (!iid.belongsTo(getRequestedAccount(zsc)))
/*     */     {
/*  98 */       return proxyRequest(request, context, iid.getAccountId());
/*     */     }
/*     */     
/* 101 */     Element msgElem = request.getElement("m");
/* 102 */     ParseMimeMessage.MimeMessageData parsedMessageData = new ParseMimeMessage.MimeMessageData();
/* 103 */     MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, mbox, msgElem, null, ParseMimeMessage.NO_INV_ALLOWED_PARSER, parsedMessageData);
/*     */     
/*     */ 
/* 106 */     Element exc = request.getOptionalElement("exceptId");
/* 107 */     Element tzElem = request.getOptionalElement("tz");
/* 108 */     CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
/* 109 */     if (calItem == null) {
/* 110 */       throw MailServiceException.NO_SUCH_CALITEM(iid.toString(), "Could not find calendar item");
/*     */     }
/* 112 */     RecurId rid = null;
/* 113 */     if (exc != null) {
/* 114 */       TimeZoneMap tzmap = calItem.getTimeZoneMap();
/* 115 */       ICalTimeZone tz = null;
/* 116 */       if (tzElem != null) {
/* 117 */         tz = CalendarUtils.parseTzElement(tzElem);
/* 118 */         tzmap.add(tz);
/*     */       }
/* 120 */       ParsedDateTime exceptDt = CalendarUtils.parseDateTime(exc, tzmap);
/* 121 */       rid = new RecurId(exceptDt, RecurId.RANGE_NONE);
/*     */     }
/* 123 */     Pair<List<MimeMessage>, List<MimeMessage>> mimePair = forwardCalItem(mbox, octxt, calItem, rid, mm, senderAcct);
/* 124 */     sendForwardMessages(mbox, octxt, mimePair);
/* 125 */     Element response = getResponseElement(zsc);
/* 126 */     return response;
/*     */   }
/*     */   
/*     */   public static void sendForwardMessages(Mailbox mbox, OperationContext octxt, Pair<List<MimeMessage>, List<MimeMessage>> pair) throws ServiceException
/*     */   {
/* 131 */     List<MimeMessage> fwdMsgs = (List)pair.getFirst();
/* 132 */     List<MimeMessage> notifyMsgs = (List)pair.getSecond();
/* 133 */     if (fwdMsgs != null) {
/* 134 */       for (MimeMessage mmFwd : fwdMsgs) {
/* 135 */         sendFwdMsg(octxt, mbox, mmFwd);
/*     */       }
/*     */     }
/* 138 */     if (notifyMsgs != null) {
/* 139 */       for (MimeMessage mmNotify : notifyMsgs)
/*     */       {
/* 141 */         sendFwdNotifyMsg(octxt, mbox, mmNotify);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static Pair<List<MimeMessage>, List<MimeMessage>> forwardCalItem(Mailbox mbox, OperationContext octxt, CalendarItem calItem, RecurId rid, MimeMessage mm, Account senderAcct) throws ServiceException {
/* 147 */     List<MimeMessage> fwdMsgs = new ArrayList();
/* 148 */     List<MimeMessage> notifyMsgs = new ArrayList();
/* 149 */     Pair<List<MimeMessage>, List<MimeMessage>> pair = new Pair(fwdMsgs, notifyMsgs);
/* 150 */     mbox.lock.lock();
/*     */     try {
/* 152 */       if (rid == null)
/*     */       {
/* 154 */         pair = getSeriesFwdMsgs(octxt, senderAcct, calItem, mm);
/*     */       }
/*     */       else {
/* 157 */         Invite inv = calItem.getInvite(rid);
/* 158 */         MimeMessage mmInv = null;
/* 159 */         if (inv != null) {
/* 160 */           mmInv = calItem.getSubpartMessage(inv.getMailItemId());
/*     */         } else {
/* 162 */           assert (rid != null);
/*     */           
/*     */ 
/* 165 */           Invite seriesInv = calItem.getDefaultInviteOrNull();
/* 166 */           if (seriesInv == null)
/* 167 */             throw ServiceException.INVALID_REQUEST("Instance specified but no recurrence series found", null);
/* 168 */           Invite exceptInv = seriesInv.newCopy();
/* 169 */           exceptInv.clearAlarms();
/* 170 */           exceptInv.setRecurrence(null);
/* 171 */           exceptInv.setRecurId(rid);
/* 172 */           long now = octxt != null ? octxt.getTimestamp() : System.currentTimeMillis();
/* 173 */           exceptInv.setDtStamp(now);
/* 174 */           ParsedDateTime dtStart = rid.getDt();
/* 175 */           ParsedDateTime dtEnd = dtStart.add(exceptInv.getEffectiveDuration());
/* 176 */           exceptInv.setDtStart(dtStart);
/* 177 */           exceptInv.setDtEnd(dtEnd);
/* 178 */           inv = exceptInv;
/*     */           
/*     */ 
/* 181 */           mmInv = calItem.getSubpartMessage(seriesInv.getMailItemId());
/*     */         }
/* 183 */         ZCalendar.ZVCalendar cal = inv.newToICalendar(true);
/* 184 */         Pair<MimeMessage, MimeMessage> instancePair = getInstanceFwdMsg(senderAcct, inv, cal, mmInv, mm);
/* 185 */         if (instancePair.getFirst() != null) {
/* 186 */           fwdMsgs.add(instancePair.getFirst());
/*     */         }
/* 188 */         if (instancePair.getSecond() != null) {
/* 189 */           notifyMsgs.add(instancePair.getSecond());
/*     */         }
/*     */       }
/*     */     } finally {
/* 193 */       mbox.lock.release();
/*     */     }
/* 195 */     return pair;
/*     */   }
/*     */   
/*     */   protected static ItemId sendFwdMsg(OperationContext octxt, Mailbox mbox, MimeMessage mmFwd) throws ServiceException
/*     */   {
/* 200 */     return CalendarMailSender.sendPartial(octxt, mbox, mmFwd, null, null, null, null, false);
/*     */   }
/*     */   
/*     */   protected static ItemId sendFwdNotifyMsg(OperationContext octxt, Mailbox mbox, MimeMessage mmFwd) throws ServiceException
/*     */   {
/* 205 */     return CalendarMailSender.sendPartial(octxt, mbox, mmFwd, null, null, null, null, false, true);
/*     */   }
/*     */   
/*     */ 
/*     */   private static Pair<List<MimeMessage>, List<MimeMessage>> getSeriesFwdMsgs(OperationContext octxt, Account senderAcct, CalendarItem calItem, MimeMessage mmFwdWrapper)
/*     */     throws ServiceException
/*     */   {
/* 212 */     DescDetectVisitor visitor = new DescDetectVisitor();
/*     */     try {
/* 214 */       visitor.accept(mmFwdWrapper);
/*     */     } catch (MessagingException e) {
/* 216 */       throw ServiceException.FAILURE("Messaging Exception while retrieving description text", e);
/*     */     }
/*     */     
/* 219 */     MimeBodyPart plainDescPart = visitor.getPlainDescPart();
/* 220 */     MimeBodyPart htmlDescPart = visitor.getHtmlDescPart();
/*     */     try
/*     */     {
/* 223 */       List<MimeMessage> msgs = new ArrayList();
/* 224 */       List<MimeMessage> notifyMsgs = new ArrayList();
/* 225 */       long now = octxt != null ? octxt.getTimestamp() : System.currentTimeMillis();
/* 226 */       Invite[] invites = calItem.getInvites();
/*     */       
/* 228 */       List<Invite> cancels = new ArrayList();
/* 229 */       for (Invite inv : invites) {
/* 230 */         if ((inv.isCancel()) && (inv.hasRecurId()) && (inviteIsAfterTime(inv, now))) {
/* 231 */           cancels.add(inv);
/*     */         }
/*     */       }
/* 234 */       LinkedList<Invite> invOrderedList = new LinkedList();
/* 235 */       for (Invite inv : invites)
/*     */       {
/* 237 */         if ((!inv.hasRecurId()) || (inviteIsAfterTime(inv, now)))
/*     */         {
/* 239 */           if (!inv.isCancel())
/* 240 */             if (inv.isRecurrence()) {
/* 241 */               invOrderedList.addFirst(inv);
/*     */             } else
/* 243 */               invOrderedList.addLast(inv);
/*     */         }
/*     */       }
/* 246 */       boolean didCancels = false;
/* 247 */       boolean firstInv = true;
/* 248 */       for (Invite inv : invOrderedList)
/*     */       {
/* 250 */         ZCalendar.ZVCalendar cal = inv.newToICalendar(true);
/*     */         
/* 252 */         if ((inv.isRecurrence()) && (!didCancels)) {
/* 253 */           didCancels = true;
/* 254 */           for (Invite cancel : cancels) {
/* 255 */             ZCalendar.ZComponent cancelComp = cancel.newToVComponent(true, true);
/* 256 */             cal.addComponent(cancelComp);
/*     */           }
/*     */         }
/*     */         
/* 260 */         MimeMessage mmInv = calItem.getSubpartMessage(inv.getMailItemId());
/* 261 */         Pair<MimeMessage, MimeMessage> fwdMsgPair = makeFwdMsg(senderAcct, inv, mmInv, cal, mmFwdWrapper, plainDescPart, htmlDescPart, firstInv);
/*     */         
/*     */ 
/* 264 */         if (fwdMsgPair.getFirst() != null) {
/* 265 */           msgs.add(fwdMsgPair.getFirst());
/*     */         }
/* 267 */         if (fwdMsgPair.getSecond() != null) {
/* 268 */           notifyMsgs.add(fwdMsgPair.getSecond());
/*     */         }
/* 270 */         firstInv = false;
/*     */       }
/* 272 */       return new Pair(msgs, notifyMsgs);
/*     */     } catch (IOException e) {
/* 274 */       throw ServiceException.FAILURE("error creating forward message", e);
/*     */     } catch (MessagingException e) {
/* 276 */       throw ServiceException.FAILURE("error creating forward message", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static void setSentByAndAttendees(ZCalendar.ZVCalendar cal, String sentBy, Address[] rcpts)
/*     */     throws ServiceException
/*     */   {
/* 285 */     String sentByAddr = "mailto:" + sentBy;
/* 286 */     for (Iterator<ZCalendar.ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext();) {
/* 287 */       ZCalendar.ZComponent comp = (ZCalendar.ZComponent)compIter.next();
/* 288 */       ZCalendar.ICalTok compName = ZCalendar.ICalTok.lookup(comp.getName());
/* 289 */       if ((ZCalendar.ICalTok.VEVENT.equals(compName)) || (ZCalendar.ICalTok.VTODO.equals(compName)))
/*     */       {
/* 291 */         for (Iterator<ZCalendar.ZProperty> propIter = comp.getPropertyIterator(); propIter.hasNext();) {
/* 292 */           ZCalendar.ZProperty prop = (ZCalendar.ZProperty)propIter.next();
/* 293 */           if ((ZCalendar.ICalTok.ATTENDEE.equals(prop.getToken())) || ("X-MS-OLK-SENDER".equalsIgnoreCase(prop.getName())))
/*     */           {
/* 295 */             propIter.remove();
/*     */           }
/*     */         }
/* 298 */         ZCalendar.ZProperty orgProp = comp.getProperty(ZCalendar.ICalTok.ORGANIZER);
/* 299 */         if (orgProp != null) {
/* 300 */           ZCalendar.ZParameter sentByParam = orgProp.getParameter(ZCalendar.ICalTok.SENT_BY);
/* 301 */           if (sentByParam != null) {
/* 302 */             sentByParam.setValue(sentByAddr);
/*     */           } else {
/* 304 */             sentByParam = new ZCalendar.ZParameter(ZCalendar.ICalTok.SENT_BY, sentByAddr);
/* 305 */             orgProp.addParameter(sentByParam);
/*     */           }
/*     */           
/* 308 */           ZCalendar.ZProperty xMsOlkSender = new ZCalendar.ZProperty("X-MS-OLK-SENDER");
/* 309 */           xMsOlkSender.setValue(sentByAddr);
/* 310 */           comp.addProperty(xMsOlkSender);
/*     */         }
/*     */         
/* 313 */         if (rcpts == null)
/* 314 */           throw ServiceException.INVALID_REQUEST("Missing forwardees", null);
/* 315 */         for (Address r : rcpts) {
/* 316 */           InternetAddress rcpt = (InternetAddress)r;
/* 317 */           String email = "mailto:" + rcpt.getAddress();
/* 318 */           ZCalendar.ZProperty att = new ZCalendar.ZProperty(ZCalendar.ICalTok.ATTENDEE, email);
/* 319 */           String name = rcpt.getPersonal();
/* 320 */           if ((name != null) && (name.length() > 0))
/* 321 */             att.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.CN, name));
/* 322 */           att.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.PARTSTAT, ZCalendar.ICalTok.NEEDS_ACTION.toString()));
/* 323 */           att.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.RSVP, "TRUE"));
/* 324 */           comp.addProperty(att);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static void setDescProps(ZCalendar.ZVCalendar cal, String descPlain, String descHtml) {
/* 331 */     for (Iterator<ZCalendar.ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext();) {
/* 332 */       ZCalendar.ZComponent comp = (ZCalendar.ZComponent)compIter.next();
/* 333 */       ZCalendar.ICalTok compName = ZCalendar.ICalTok.lookup(comp.getName());
/* 334 */       if ((ZCalendar.ICalTok.VEVENT.equals(compName)) || (ZCalendar.ICalTok.VTODO.equals(compName)))
/*     */       {
/* 336 */         for (Iterator<ZCalendar.ZProperty> propIter = comp.getPropertyIterator(); propIter.hasNext();) {
/* 337 */           ZCalendar.ZProperty prop = (ZCalendar.ZProperty)propIter.next();
/* 338 */           ZCalendar.ICalTok tok = prop.getToken();
/* 339 */           if ((ZCalendar.ICalTok.DESCRIPTION.equals(tok)) || (ZCalendar.ICalTok.X_ALT_DESC.equals(tok))) {
/* 340 */             propIter.remove();
/*     */           }
/*     */         }
/* 343 */         if ((descPlain != null) && (descPlain.length() > 0)) {
/* 344 */           comp.addProperty(new ZCalendar.ZProperty(ZCalendar.ICalTok.DESCRIPTION, descPlain));
/*     */         }
/* 346 */         if ((descHtml == null) || (descHtml.length() <= 0)) break;
/* 347 */         ZCalendar.ZProperty prop = new ZCalendar.ZProperty(ZCalendar.ICalTok.X_ALT_DESC, descHtml);
/* 348 */         prop.addParameter(new ZCalendar.ZParameter(ZCalendar.ICalTok.FMTTYPE, "text/html"));
/* 349 */         comp.addProperty(prop);
/* 350 */         break;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   protected static Pair<MimeMessage, MimeMessage> getInstanceFwdMsg(Account senderAcct, Invite inv, ZCalendar.ZVCalendar cal, MimeMessage mmInv, MimeMessage mmFwdWrapper)
/*     */     throws ServiceException
/*     */   {
/* 360 */     DescDetectVisitor visitor = new DescDetectVisitor();
/*     */     try {
/* 362 */       visitor.accept(mmFwdWrapper);
/*     */     } catch (MessagingException e) {
/* 364 */       throw ServiceException.FAILURE("Messaging Exception while retrieving description text", e);
/*     */     }
/*     */     
/* 367 */     MimeBodyPart plainDescPart = visitor.getPlainDescPart();
/* 368 */     MimeBodyPart htmlDescPart = visitor.getHtmlDescPart();
/*     */     try
/*     */     {
/* 371 */       return makeFwdMsg(senderAcct, inv, mmInv, cal, mmFwdWrapper, plainDescPart, htmlDescPart, true);
/*     */     } catch (IOException e) {
/* 373 */       throw ServiceException.FAILURE("error creating forward message", e);
/*     */     } catch (MessagingException e) {
/* 375 */       throw ServiceException.FAILURE("error creating forward message", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static Pair<MimeMessage, MimeMessage> makeFwdMsg(Account senderAcct, Invite inv, MimeMessage mmInv, ZCalendar.ZVCalendar cal, MimeMessage mmFwdWrapper, MimeBodyPart plainDesc, MimeBodyPart htmlDesc, boolean useFwdText)
/*     */     throws ServiceException, MessagingException, IOException
/*     */   {
/* 386 */     setSentByAndAttendees(cal, AccountUtil.getFriendlyEmailAddress(senderAcct).getAddress(), mmFwdWrapper.getAllRecipients());
/*     */     
/*     */ 
/*     */ 
/* 390 */     Address from = null;
/* 391 */     Address sender = null;
/* 392 */     sender = AccountUtil.getFriendlyEmailAddress(senderAcct);
/* 393 */     ZOrganizer org = inv.getOrganizer();
/* 394 */     if (org != null) {
/* 395 */       if (org.hasCn()) {
/* 396 */         from = new JavaMailInternetAddress(org.getAddress(), org.getCn(), "utf-8");
/*     */       } else
/* 398 */         from = new JavaMailInternetAddress(org.getAddress());
/*     */     } else {
/* 400 */       from = sender;
/*     */     }
/*     */     MimeMessage mm;
/*     */     MimeMessage mm;
/* 404 */     if (useFwdText) {
/* 405 */       String plainDescStr = null;
/* 406 */       String htmlDescStr = null;
/* 407 */       if (plainDesc != null)
/* 408 */         plainDescStr = (String)plainDesc.getContent();
/* 409 */       if (htmlDesc != null)
/* 410 */         htmlDescStr = (String)htmlDesc.getContent();
/* 411 */       setDescProps(cal, plainDescStr, htmlDescStr);
/* 412 */       mm = createMergedMessage(senderAcct, from, sender, mmInv, inv, cal, plainDesc, htmlDesc);
/*     */     } else {
/* 414 */       mm = CalendarMailSender.createCalendarMessage(senderAcct, from, sender, null, mmInv, inv, cal, false);
/*     */     }
/*     */     
/* 417 */     Message.RecipientType[] rcptTypes = { Message.RecipientType.TO, Message.RecipientType.CC, Message.RecipientType.BCC };
/* 418 */     for (Message.RecipientType rcptType : rcptTypes) {
/* 419 */       Address[] rcpts = mmFwdWrapper.getRecipients(rcptType);
/* 420 */       mm.setRecipients(rcptType, rcpts);
/*     */     }
/* 422 */     mm.setSubject(mmFwdWrapper.getSubject());
/* 423 */     mm.saveChanges();
/*     */     
/*     */ 
/* 426 */     MimeMessage notifyMimeMsg = null;
/* 427 */     if (org != null) {
/* 428 */       String orgAddress = org.getAddress();
/* 429 */       Account orgAccount = Provisioning.getInstance().getAccountByName(orgAddress);
/* 430 */       if ((orgAccount != null) && (!orgAccount.getId().equals(senderAcct.getId()))) {
/* 431 */         notifyMimeMsg = CalendarMailSender.createForwardNotifyMessage(senderAcct, orgAccount, orgAddress, mmFwdWrapper.getAllRecipients(), inv);
/*     */       }
/*     */     }
/*     */     
/* 435 */     return new Pair(mm, notifyMimeMsg);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private static MimeMessage createMergedMessage(Account account, Address fromAddr, Address senderAddr, MimeMessage mmInv, Invite inv, ZCalendar.ZVCalendar cal, MimeBodyPart plainDescPart, MimeBodyPart htmlDescPart)
/*     */     throws ServiceException
/*     */   {
/*     */     try
/*     */     {
/* 446 */       String uid = inv.getUid();
/* 447 */       if (mmInv != null) {
/* 448 */         MimeMessage mm = new ZMimeMessage(mmInv);
/*     */         
/*     */ 
/* 451 */         Enumeration eh = mmInv.getAllHeaders();
/* 452 */         while (eh.hasMoreElements()) {
/* 453 */           Header hdr = (Header)eh.nextElement();
/* 454 */           String hdrNameUpper = hdr.getName().toUpperCase();
/* 455 */           if ((!hdrNameUpper.startsWith("CONTENT-")) && (!hdrNameUpper.equals("SUBJECT"))) {
/* 456 */             mm.removeHeader(hdr.getName());
/*     */           }
/*     */         }
/*     */         
/* 460 */         mm.setSentDate(new Date());
/* 461 */         mm.setRecipients(Message.RecipientType.TO, (Address[])null);
/* 462 */         mm.setRecipients(Message.RecipientType.CC, (Address[])null);
/* 463 */         mm.setRecipients(Message.RecipientType.BCC, (Address[])null);
/*     */         
/* 465 */         if (fromAddr != null)
/* 466 */           mm.setFrom(fromAddr);
/* 467 */         if (senderAddr != null) {
/* 468 */           mm.setSender(senderAddr);
/*     */         }
/*     */         
/* 471 */         ReplacingVisitor visitor = new ReplacingVisitor(uid, cal, plainDescPart, htmlDescPart);
/* 472 */         visitor.accept(mm);
/*     */         
/* 474 */         mm.saveChanges();
/* 475 */         return mm;
/*     */       }
/* 477 */       String subject = inv.getName();
/*     */       String desc;
/*     */       String descHtml;
/* 480 */       try { Object plainContent = plainDescPart != null ? plainDescPart.getContent() : null;
/* 481 */         desc = plainContent != null ? plainContent.toString() : null;
/* 482 */         Object htmlContent = htmlDescPart != null ? htmlDescPart.getContent() : null;
/* 483 */         descHtml = htmlContent != null ? htmlContent.toString() : null;
/*     */       } catch (MessagingException e) {
/* 485 */         throw ServiceException.FAILURE("Messaging Exception while retrieving description", e);
/*     */       }
/*     */       catch (IOException e) {
/* 488 */         throw ServiceException.FAILURE("Messaging Exception while retrieving description", e);
/*     */       }
/*     */       
/* 491 */       return CalendarMailSender.createCalendarMessage(account, fromAddr, senderAddr, null, subject, desc, descHtml, uid, cal, false);
/*     */     }
/*     */     catch (MessagingException e) {
/* 494 */       throw ServiceException.FAILURE("Messaging Exception while building calendar message from source MimeMessage", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static class DescDetectVisitor
/*     */     extends MimeVisitor
/*     */   {
/*     */     private MimeBodyPart mPlainPart;
/*     */     
/*     */ 
/*     */     private MimeBodyPart mHtmlPart;
/*     */     
/* 508 */     public MimeBodyPart getPlainDescPart() { return this.mPlainPart; }
/* 509 */     public MimeBodyPart getHtmlDescPart() { return this.mHtmlPart; }
/*     */     
/*     */     private static boolean matchingType(Part part, String ct) throws MessagingException {
/* 512 */       String mmCtStr = part.getContentType();
/* 513 */       if (mmCtStr != null) {
/* 514 */         ContentType mmCt = new ContentType(mmCtStr);
/* 515 */         return mmCt.match(ct);
/*     */       }
/* 517 */       return false;
/*     */     }
/*     */     
/*     */     protected boolean visitBodyPart(MimeBodyPart bp) throws MessagingException
/*     */     {
/* 522 */       if ((this.mPlainPart == null) && (matchingType(bp, "text/plain")))
/* 523 */         this.mPlainPart = bp;
/* 524 */       if ((this.mHtmlPart == null) && (matchingType(bp, "text/html")))
/* 525 */         this.mHtmlPart = bp;
/* 526 */       return false;
/*     */     }
/*     */     
/*     */     protected boolean visitMessage(MimeMessage mm, MimeVisitor.VisitPhase visitKind) throws MessagingException
/*     */     {
/* 531 */       return false;
/*     */     }
/*     */     
/*     */     protected boolean visitMultipart(MimeMultipart mp, MimeVisitor.VisitPhase visitKind) throws MessagingException
/*     */     {
/* 536 */       return false;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class ReplacingVisitor
/*     */     extends MimeVisitor
/*     */   {
/*     */     private final String mUid;
/*     */     private final ZCalendar.ZVCalendar mCalNew;
/*     */     private final MimeBodyPart mPlainNew;
/*     */     private final MimeBodyPart mHtmlNew;
/*     */     private MimeBodyPart mCalendarPart;
/*     */     private MimeBodyPart mPlainPart;
/*     */     private MimeBodyPart mHtmlPart;
/*     */     private boolean mCalendarPartReplaced;
/*     */     private boolean mPlainPartReplaced;
/*     */     private boolean mHtmlPartReplaced;
/*     */     
/*     */     public ReplacingVisitor(String uid, ZCalendar.ZVCalendar cal, MimeBodyPart plainNew, MimeBodyPart htmlNew)
/*     */     {
/* 556 */       this.mUid = uid;
/* 557 */       this.mCalNew = cal;
/* 558 */       this.mPlainNew = plainNew;
/* 559 */       this.mHtmlNew = htmlNew;
/*     */     }
/*     */     
/*     */     private static boolean matchingType(Part part, String ct) throws MessagingException {
/* 563 */       String mmCtStr = part.getContentType();
/* 564 */       if (mmCtStr != null) {
/* 565 */         ContentType mmCt = new ContentType(mmCtStr);
/* 566 */         return mmCt.match(ct);
/*     */       }
/* 568 */       return false;
/*     */     }
/*     */     
/*     */     protected boolean visitBodyPart(MimeBodyPart bp)
/*     */       throws MessagingException
/*     */     {
/* 574 */       if ((this.mCalendarPart == null) && (matchingType(bp, "text/calendar")))
/* 575 */         this.mCalendarPart = bp;
/* 576 */       if ((this.mPlainPart == null) && (matchingType(bp, "text/plain")))
/* 577 */         this.mPlainPart = bp;
/* 578 */       if ((this.mHtmlPart == null) && (matchingType(bp, "text/html")))
/* 579 */         this.mHtmlPart = bp;
/* 580 */       return false;
/*     */     }
/*     */     
/*     */     protected boolean visitMessage(MimeMessage mm, MimeVisitor.VisitPhase visitKind) throws MessagingException
/*     */     {
/* 585 */       if (MimeVisitor.VisitPhase.VISIT_END.equals(visitKind)) {
/* 586 */         if (!this.mCalendarPartReplaced)
/*     */         {
/*     */ 
/* 589 */           setCalendarContent(mm, this.mCalNew);
/*     */         }
/* 591 */         return true;
/*     */       }
/* 593 */       return false;
/*     */     }
/*     */     
/*     */     private static void setCalendarContent(Part part, ZCalendar.ZVCalendar cal) throws MessagingException
/*     */     {
/* 598 */       String filename = "meeting.ics";
/* 599 */       part.setDataHandler(new DataHandler(new CalendarDataSource(cal, filename)));
/*     */     }
/*     */     
/*     */     protected boolean visitMultipart(MimeMultipart mp, MimeVisitor.VisitPhase visitKind) throws MessagingException
/*     */     {
/* 604 */       boolean modified = false;
/* 605 */       if (MimeVisitor.VisitPhase.VISIT_END.equals(visitKind)) {
/* 606 */         if ((!this.mPlainPartReplaced) && (this.mPlainPart != null)) {
/* 607 */           modified = true;
/*     */           
/*     */ 
/* 610 */           if (mp.removeBodyPart(this.mPlainPart)) {
/* 611 */             if (this.mPlainNew != null)
/* 612 */               mp.addBodyPart(this.mPlainNew);
/* 613 */             this.mPlainPartReplaced = true;
/*     */           }
/*     */         }
/* 616 */         if ((!this.mHtmlPartReplaced) && (this.mHtmlPart != null)) {
/* 617 */           modified = true;
/*     */           
/*     */ 
/* 620 */           if (mp.removeBodyPart(this.mHtmlPart)) {
/* 621 */             if (this.mHtmlNew != null)
/* 622 */               mp.addBodyPart(this.mHtmlNew);
/* 623 */             this.mHtmlPartReplaced = true;
/*     */           }
/*     */         }
/* 626 */         if ((!this.mCalendarPartReplaced) && (this.mCalendarPart != null)) {
/* 627 */           modified = true;
/*     */           
/*     */ 
/* 630 */           if (mp.removeBodyPart(this.mCalendarPart)) {
/* 631 */             MimeBodyPart newCalendarPart = new ZMimeBodyPart();
/* 632 */             setCalendarContent(newCalendarPart, this.mCalNew);
/* 633 */             mp.addBodyPart(newCalendarPart);
/* 634 */             this.mCalendarPartReplaced = true;
/*     */           } else {
/* 636 */             throw new MessagingException("Unable to remove old calendar part");
/*     */           }
/*     */         }
/*     */       }
/* 640 */       return modified;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/ForwardCalendarItem.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */